package com.wen.task.baseTasks;

import com.wen.entry.Result;
import com.wen.moudle.Account;
import com.wen.moudle.Managers.FightManager;
import com.wen.moudle.Managers.ShopManager;
import com.wen.task.AbstractTask;
import com.wen.utils.ConsoleUtils;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AutoExperienceArenaTask extends AbstractTask {
    private ShopManager shopManager;
    private FightManager fightManager;
    public AutoExperienceArenaTask(Account account) {
        super(account);
        fightManager = getFightManager();
        shopManager = getShopManager();
    }

    @Override
    public void run() {
        log.info("开始挂机普通经验竞技场");
        Integer id = shopManager.getCommodityId("英雄帖");
        int count = 0;

        System.out.println("英雄帖不足时是否需要自动购买英雄帖?[y/n]");
        boolean flag = ConsoleUtils.yesOrNo();

        while (true){
            Result<String> result = fightManager.experienceArena();
            if (result.isSuccess()) {
                // 只要成功，继续挑战
                log.info("竞技场第 {} 次报名成功, 正在匹配对手", ++count);
                continue;
            }else if (result.getErrorMessage().contains("进行中")){
                try {
                    Thread.sleep(15000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if (flag && result.getErrorMessage().contains("英雄帖")) {
                Result<String> purchase = shopManager.purchase(id, 1);
                if (purchase.isSuccess()) continue;
                log.error("{}", purchase.getErrorMessage());
                break;
            } else {
                log.error("{}", result.getErrorMessage());
                break;
            }
            log.info("本次共进行了 {} 场普通经验竞技场", count);
        }
    }

    @Override
    public String getTitle() {
        return "一键挂机经验竞技场";
    }
}
